<template>
  <c-form :inline="true" :form="form" ref="refForm"></c-form>
  <el-dialog
    title="选择抄送人"
    v-model="visible"
    append-to-body
    destroy-on-close
    width="60%"
  >
    <c-general-copies
      ref="copiesField"
      :copiesData="copiesData"
      :roleCopiesData="roleCopiesData"
    ></c-general-copies>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitFields">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { getServiceExecuteList } from "@/api/bpmnApi";
import Form from "@/components/Form";
import GeneralCopies from "@/components/bpmn/components/bpmnPropertiesPanel/components/generalFormComponents/userTaskComponents/GeneralComponents/GeneralCopies.vue";
import { TaskForm } from "@/components/bpmn/components/bpmnPropertiesPanel/forms";

export default {
  name: "ServiceTaskClassField",
  components: {
    "c-form": Form,
    "c-general-copies": GeneralCopies,
  },
  props: {
    businessObj: {
      type: Object,
    },
    modeler: {
      type: Object,
    },
  },
  data() {
    return {
      visible: false,
      form: TaskForm.call(this),
      formatFieldsData: [],
      copiesData: [],
      roleCopiesData: [],
    };
  },
  methods: {
    fieldsDialogDataReset() {
      this.visible = true;
    },
    // 保存设置字段
    async submitFields() {
      this.visible = false;
      const length = this.$refs.copiesField.copiesData?.length;
      const roleLength = this.$refs.copiesField.roleCopiesData?.length;
      this.form.items.fields.options =
        length > 0 ? [{ key: 1, label: length + "抄送人" }] : [];

      if (roleLength > 0) {
        this.form.items.fields.options.push({
          key: 1,
          label: roleLength + "业务角色",
        });
      }
      if (length > 0) {
        let numName = "";
        for (let i in this.$refs.copiesField.copiesData) {
          numName += this.$refs.copiesField.copiesData[i] + ",";
        }
        numName = numName.substring(0, numName.length - 1);
        this.formatFieldsData.push({
          elementName: "activiti:Field",
          properties: {
            name: "assignee",
            ["expression"]: numName,
          },
        });
        if (roleLength == 0) {
          this.formatFieldsData.push({
            elementName: "activiti:Field",
            properties: {
              name: "role",
              ["expression"]: "",
            },
          });
          this.formatFieldsData.push({
            elementName: "activiti:Field",
            properties: {
              name: "assignType",
              ["expression"]: "assignType1",
            },
          });
        }
        this.modeler.createExtensionElements(this.formatFieldsData);
      }
      if (roleLength > 0) {
        let roleName = "";
        for (let i in this.$refs.copiesField.roleCopiesData) {
          roleName += this.$refs.copiesField.roleCopiesData[i] + ",";
        }
        roleName = roleName.substring(0, roleName.length - 1);
        this.formatFieldsData.push({
          elementName: "activiti:Field",
          properties: {
            name: "role",
            ["expression"]: roleName,
          },
        });
        if (length == 0) {
          this.formatFieldsData.push({
            elementName: "activiti:Field",
            properties: {
              name: "assignee",
              ["expression"]: "",
            },
          });
          this.formatFieldsData.push({
            elementName: "activiti:Field",
            properties: {
              name: "assignType",
              ["expression"]: "assignType0",
            },
          });
        }

        this.modeler.createExtensionElements(this.formatFieldsData);
      }
      if (roleLength > 0 && length > 0) {
        this.formatFieldsData.push({
          elementName: "activiti:Field",
          properties: {
            name: "assignType",
            ["expression"]: "assignType2",
          },
        });
        this.modeler.createExtensionElements(this.formatFieldsData);
      }
    },
  },
  async mounted() {
    const rst = await getServiceExecuteList({});
    if (!rst?.success) {
      this.$message({
        type: "warning",
        message: rst.message,
      });
    }
    // this.form.items.class.options = rst.data.map((item) => ({
    //   value: item.executeClass,
    //   label: item.executeName,
    // }));
    // this.form.models.class = this.modeler.getProperty("class");
    const length = this.businessObj?.extensionElements?.values?.length || 0;
    if (length > 0) {
      this.businessObj?.extensionElements?.values.map((item) => {
        if (item.name == "assignee" && item.expression !== "") {
          this.copiesData=item.expression.split(",")
          this.form.items.fields.options =
            length > 0
              ? [
                  {
                    key: 1,
                    label: item.expression.split(",").length + "抄送人",
                  },
                ]
              : [];
        } else if (item.name == "role" && item.expression !== "") {
          this.roleCopiesData=item.expression.split(",")
          this.form.items.fields.options.push({
            key: 1,
            label: item.expression.split(",").length + "角色",
          });
        }
      });
    }
  },
};
</script>
